#include "ConfCard.h"
#include "ui_ConfCard.h"

#include "MorphoCard.h"

ConfCard::ConfCard(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ConfCard)
{
    ui->setupUi(this);
    this->setFixedSize(this->width(), this->height());
    this->move(QApplication::desktop()->availableGeometry().center() - this->rect().center());
    /* 1.0 */
    fillComboBoxLector();
}

ConfCard::~ConfCard()
{
    delete ui;
}

void ConfCard::fillComboBoxLector()
{
    /* 1.1 */
    MorphoCard card;
    /* 1.2 */
    readers = card.getReaders();
    if ( !readers.empty() )
        for (size_t i = 0; i < readers.size(); i++)
            ui->comboBoxLector->addItem(QString::fromStdString(readers[i]));
}

void ConfCard::readData()
{
    MorphoCard  card;
//    if (!card.connectCardToSelectedReader(ui->comboBoxLector->currentText().toStdString())) return;
    int r = ui->comboBoxLector->currentIndex();
//    cout << "reader # = " << r << endl;
    string data = card.leerSmartCard(r);
    if (data.size() != 64)  return;

    cout << "CARD DATA : " << data.size() << endl;
    cout << "----------------------------------------------" << endl;
    cout<<"--->card.fechaIni()  : "<<data.substr(0,8)<<endl;
    cout<<"--->card.fechaFin()  : "<<data.substr(8,8)<<endl;
    cout<<"--->card.tipo()      : "<<data[16]<<endl;
    //cout<<"--->card.secuencia(): "<<data_[17]<<endl;
    cout<<"--->card.valor()     : "<<data[18]<<endl;
    cout<<"--->card.mesa()      : "<<data.substr(19, 6)<<endl;
    cout<<"--->card.numserie()  : "<<data.substr(48, 16)<<endl;
    cout << "----------------------------------------------" << endl;
    ui->dateEditFechaInicial->setDate(QDate::fromString(QString::fromStdString(data.substr(0,8)), "ddMMyyyy"));
    ui->dateEditFechaFinal->setDate(QDate::fromString(QString::fromStdString(data.substr(8,8)), "ddMMyyyy"));
    if      ( data[16] == 'A' ) ui->comboBoxTipo->setCurrentIndex(0);
    else if ( data[16] == 'D' ) ui->comboBoxTipo->setCurrentIndex(1);
    else if ( data[16] == 'E' ) ui->comboBoxTipo->setCurrentIndex(2);
    if      ( data[18] == '0' ) ui->comboBoxValor->setCurrentIndex(0);
    else if ( data[18] == 0x00 || data[18] == 0x40 ) ui->comboBoxValor->setCurrentIndex(1);
    ui->lineEditNumeroMesa->setText(QString::fromStdString(data.substr(19, 6)));
}

void ConfCard::readCard()
{
    cout << RED << "READ CARD" << RESET << endl;
    readData();
}

void ConfCard::setInfoCard()
{
//    cout << "setInfoCard()" << endl;
    cout << RED << "CONFIGURAR CARD" << RESET << endl;
    cout << ">>>----------------------------------------------" << endl;
    string fechaIni = ui->dateEditFechaInicial->date().toString("ddMMyyyy").toStdString();
    string fechaFin = ui->dateEditFechaFinal->date().toString("ddMMyyyy").toStdString();

    cout << "fechaIni   : "<<fechaIni<<" : "<<fechaIni.size()<<endl;
    cout << "fechaFin   : "<<fechaFin<<" : "<<fechaFin.size()<<endl;

//    cout<<"fechaIni: "<<fechaIni[0]<<" : "<<fechaIni[1]<<" : "<<fechaIni[2]<<" : "<<fechaIni[3]<<" : "<<fechaIni[4]<<endl;

    char tipo = 'X';
    if (ui->comboBoxTipo->currentIndex()==0)      tipo = 'A';
    else if (ui->comboBoxTipo->currentIndex()==1) tipo = 'D';
    else if (ui->comboBoxTipo->currentIndex()==2) tipo = 'E';
    cout << "tipo       : " << tipo << endl;

    string secuencia = QString::number(ui->spinBoxSecuencia->value()).toStdString();
    if (secuencia.size() != 1) return;

    char valor = 0x0;
    if (ui->comboBoxValor->currentIndex()==0)
       valor = '0';
    else
       valor = 0x0;
    cout << "valor      : " << valor << endl;

    string mesa = ui->lineEditNumeroMesa->text().toStdString();
    if (mesa.length() != 6) return;
    cout << "mesa       : " << mesa << endl;
    string zeros = "0000000";

    string header = fechaIni + fechaFin + tipo + secuencia + valor + mesa + zeros;
    cout << "header = " << header << " --- " << header.size() << endl;

    MorphoCard card;
//    if (!card.connectCardToSelectedReader(ui->comboBoxLector->currentText().toStdString())) return;
    int r = ui->comboBoxLector->currentIndex();
//    cout << "reader # = " << r << endl;
    if ( !card.setData(header, r) )    cout << RED << "failed writing !!!" << RESET << endl;
    cout << "---------------------------------------------->>>" << endl;
}

void ConfCard::cleanCard()
{
    cout << "cleanCard()" << endl;
    MorphoCard  card;
//    /*
//    if (!card.connectCardToSelectedReader(ui->comboBoxLector->currentText().toStdString())) return;
    int r = ui->comboBoxLector->currentIndex();
//    cout << "reader # = " << r << endl;
    card.setValor('0', r);
//    */
/*
    if (!card.cardInserted())   return;
    string data = card.leerSmartCard();
    cout << "data = " << data << endl;
//*/
    //    MorphoCard card;
    //    card.clean();
    //        readData();
}
